﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DAH_TD_
{
    class LevelManager
    {
        List<Level> levels;
            public List<Level> getLevels() { return this.levels; }
            public void setLevels(List<Level> levels) { this.levels = levels; }
        Level level;
        List<Wave> waves;
        Wave wave;
        int x;

        public LevelManager()
        {
        }

        public void Init() 
        {
            levels = new List<Level>();
            level = new Level();
            waves = new List<Wave>();
            wave = new Wave();
        }


        //Dateipfad finden
        internal static string GetProjectPath()
        {
            // Ergebnis: Debug- oder Release-Ordner im Projektordner.
            string projectPath = Environment.CurrentDirectory;
            // Mit jedem Durchlauf geht es im Verzeichnisbaum eine Stufe höher.
            for (int i = 0; i <= 5; i++)
            {
                projectPath = System.IO.Path.GetDirectoryName(projectPath);
            }
            return projectPath + @"\";
        }


        public void readInLevels(int levelCount) 
        {
            string levelString1 = "Levels/Level0";
            string levelString2 = ".level";
            string levelStringCombined;
            for (int i = 1; i < levelCount + 1; i++)
            {
                levelStringCombined = levelString1 + i + levelString2;
                readInLevel(levelStringCombined);
                levels.Add(level);
            }
        }

        public bool readInLevel(string path)
        {
            level = new Level();
            waves = new List<Wave>();

            System.IO.StreamReader file;

            String currentDirectory = GetProjectPath();

            // Map-Datei öffnen
            file = new System.IO.StreamReader(Path.Combine(currentDirectory, path));

            file.ReadLine(); // Levelid folgt
            level.setId(Convert.ToInt32(file.ReadLine()));
            file.ReadLine(); //Mappfad folgt
            level.setMapDirectory(Convert.ToString(file.ReadLine()));
            file.ReadLine(); // folgend Wellenzahl
            level.setWaveCount(Convert.ToInt32(file.ReadLine()));

            // Gegnertypen einlesen
            int enemiesInNumber;
            for (int i = 0; i < level.getWaveCount(); i++)
            {
                wave = new Wave();
                file.ReadLine();
                wave.setEnemyCount(Convert.ToInt32(file.ReadLine()));
                enemiesInNumber = Convert.ToInt32(file.ReadLine());
                wave.setEnemies(enemyListOutOfNumber(enemiesInNumber, wave));
                waves.Add(wave);
            }
            level.setWaves(waves);
                     

            // Datei schließen
            file.Close();

            return true;
        }

        private List<int> enemyListOutOfNumber(int input, Wave wave) 
        {
            List<int> list = new List<int>();
            // erstmal die Zahl in Ziffern zerlegen und in Array speichern
            int [] enemyNumbers = new int[wave.getEnemyCount()];
            enemyNumbers = Extract(input);

            // Dann alle Ziffern auf Gegnerliste der Welle schreiben
            for ( int i = 0; i < wave.getEnemyCount(); i++){
                list.Add(enemyNumbers[i]);
            }
            return list;
        }

        public static int[] Extract(int number)
        {
            int multipleOfTen = 1;
            int digitCount = 1;
            while (number / multipleOfTen >= 10.0f)
            {
                multipleOfTen *= 10;
                ++digitCount;
            }
            int[] digits = new int[digitCount];
            for (int i = 0; multipleOfTen > 0; ++i, multipleOfTen /= 10)
            {
                digits[i] = (int)(number / multipleOfTen);
                number -= digits[i] * multipleOfTen;
            }

            return digits;
        }
    }
}
